<template>
  <el-dialog
      title="审核申请"
      :visible.sync="dialogVisible"
      width="500px"
      top="5vh"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="handleClose"
  >
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="审核状态" prop="review_status">
        <el-select v-model="form.review_status" placeholder="请选择审核状态" style="width: 100%">
          <el-option label="待审核" :value="1"></el-option>
          <el-option label="审核通过" :value="2"></el-option>
          <el-option label="审核拒绝" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核备注" prop="review_remark">
        <el-input
            v-model="form.review_remark"
            type="textarea"
            :rows="4"
            placeholder="请输入审核备注"
        ></el-input>
      </el-form-item>
      <el-form-item label="分配业务员" prop="assignee">
        <el-input v-model="form.assignee" placeholder="请输入分配的业务员"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { http } from "@/api";

export default {
  name: "LoanApplicationReview",
  data() {
    return {
      dialogVisible: false,
      form: {
        id: "",
        review_status: 1,
        review_remark: "",
        assignee: ""
      },
      rules: {
        review_status: [
          { required: true, message: "请选择审核状态", trigger: "change" }
        ],
        review_remark: [
          { required: true, message: "请输入审核备注", trigger: "blur" },
          { max: 500, message: "审核备注不能超过500个字符", trigger: "blur" }
        ],
        assignee: [
          { max: 64, message: "分配的业务员不能超过64个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    open(row) {
      this.dialogVisible = true;
      this.form = {
        id: row.id,
        review_status: row.review_status,
        review_remark: row.review_remark || "",
        assignee: row.assignee || ""
      };
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
    },
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            await http.loanApplication.update(this.form.id, this.form);
            this.$message.success("审核成功");
            this.handleClose();
            this.$emit("get-page");
          } catch (error) {
            console.error(error);
            this.$message.error("审核失败");
          }
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.el-dialog {
  max-height: 90vh;
  overflow: auto;
}
.el-dialog__body {
  max-height: calc(90vh - 120px);
  overflow-y: auto;
}
</style> 